package.path = "./plugin/THMD20E/utils.lua"..";..\\?.lua"
package.path = "./plugin/THMD20E/json.lua"..";..\\?.lua"

require "utils.lua"
require "json.lua"

rxBuf = {}
regAddr = 0x0000

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        -----------温湿度----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x03
        requestADU[3] = 0x00
        requestADU[4] = 0x02
        requestADU[5] = 0x00
        requestADU[6] = 0x02
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0002
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    
    end
    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    local temp = 0
    if(cmdName == "SetOpen")
    then
        -----------合闸----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x01
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        }
    elseif(cmdName == "SetClose")
    then
        -----------分闸----------------
        requestADU[1] = sAddr
        requestADU[2] = 0x06
        requestADU[3] = 0x00
        requestADU[4] = 0x00
        requestADU[5] = 0x00
        requestADU[6] = 0x00
        local VariableMapTemp = {
            Variable = requestADU
        }
        crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc%256)
        requestADU[8] = math.modf(crc/256)

        regAddr = 0x0000
        VariableMap = {
            Status = "0",
            Variable = requestADU
        } 
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local index = 1
    local mAddr = 0
    local mFunCode = 0
    local mRegByte = 0
    local crcH,crcL,crc,crcTemp

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    --modbus03 06 最短为8个字节
    if(rxBufCnt < 8)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            mAddr     = rxBuf[index]
            mFunCode  = rxBuf[index+1]
            if(mFunCode == 0x03)
            then
                mRegByte = rxBuf[index+2]
                if(mRegByte+5 > rxBufCnt)
                then
                    rxBuf = {}
                    return VariableMap
                end
                --取出数据域数据
                local pdu = {}
                for i=index,index+2+mRegByte,1 do
                    if rxBuf[i] ~= nil then
                        table.insert(pdu, rxBuf[i])
                    end
                end
                local VariableMapCRC = {
                    Variable = pdu
                }
                crc = GetCRCModbus(VariableMapCRC)
                crcTemp = rxBuf[index+3+mRegByte+1]*256+rxBuf[index+3+mRegByte]
                print(crc)
                if(crc == crcTemp)
                then
                    ---------温湿度------------
                    local Variable = {}
                    local VariableIndex = 1
                    if(regAddr == 0x0002)
                    then
                        -----------------------------温度--------------------------------
                        local tempValue = rxBuf[index+3]*256+rxBuf[index+4]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Temp",tempValue,string.format("%2.1f",tempValue/10.0))
                        VariableIndex = VariableIndex + 1
                        -----------------------------湿度--------------------------------
                        local HumiValue = rxBuf[index+5]*256+rxBuf[index+6]
                        Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Humi",HumiValue,string.format("%2.1f",HumiValue/10.0))
                        VariableIndex = VariableIndex + 1

                        VariableMap = {
                            Status = "0",--0 解析成功，1解析失败
                            Variable = Variable
                        }
                    end
                    rxBuf = {}
                    return VariableMap
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end
